package ct.zxd.servlet;


import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ct.zxd.utils.HttpsRequest;

/**
 * Servlet implementation class GetOpenID
 */
public class GetOpenID extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private final String appid = "wx8ca51e6343efcb94";
	private final String secret = "c8fbd35ba52ac42bd3690bf2d1e33e49";
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetOpenID() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String jscode = request.getParameter("jscode");
		String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+appid+
            "&secret="+secret+"&js_code="+jscode+"&grant_type=authorization_code";
		PrintWriter pw = response.getWriter();
		
		 try {
			String data = HttpsRequest.sendGetRequest(url, null);
			pw.write(data);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	  

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
